package com.hzjq.blast

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.hzjq.core.Blast
import com.hzjq.core.bean.*
import com.hzjq.core.callback.*
import com.hzjq.core.event.PioUnEnableEvent
import com.hzjq.core.receive.ReceiverInterceptor
import com.hzjq.core.util.BlastLog
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import pub.devrel.easypermissions.EasyPermissions

@Suppress("DEPRECATED_IDENTITY_EQUALS")
class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks {

    private lateinit var mLogAdapter: LogAdapter
    private val caps = arrayListOf<CapEntity>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Blast.getInstance().setDebug(true)
        mLogAdapter = LogAdapter(this)
        logView.adapter = mLogAdapter
        logView.dividerHeight = 0


        Blast.getInstance().getScannerLoader()
            .openScanner(this)
        Blast.getInstance().getScannerLoader()
            .setMode(mainScanResult,1)

        Blast.getInstance().getChecker().start()

        Blast.getInstance().addInterceptor(object : ReceiverInterceptor{
            override fun onInterceptor(stateCode: Int): Boolean {
                Blast.getInstance().getChecker().stop()
                runOnUiThread {
                    mainTitleView.text = "当前状态值:$stateCode"
                }
                BlastLog.e("stateCode:${stateCode}")
                if(stateCode == 104){
                    return true
                }
                return false
            }
        })

        onScannerCap()

        EventBus.getDefault().register(this)
        mScreenStatusReceiver = ScreenStatusReceiver()
        val screenStatusIF =  IntentFilter()
        screenStatusIF.addAction(Intent.ACTION_SCREEN_ON)
        screenStatusIF.addAction(Intent.ACTION_SCREEN_OFF)
        registerReceiver(mScreenStatusReceiver!!,screenStatusIF)
    }




    private fun reqPermissions() {
        EasyPermissions.requestPermissions(
            this,
            "打开串口，需要文件操作权限",
            111,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.READ_EXTERNAL_STORAGE
        )
    }

    private fun hasPermission(): Boolean {
        return ContextCompat.checkSelfPermission(
            this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE
        ) !== PackageManager.PERMISSION_GRANTED
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun addLog(log: LogBean) {
        if (mLogAdapter.count > 499) {
            mLogAdapter.remove(mLogAdapter.getItem(0))
        }
        mLogAdapter.add(log)
        BlastLog.e(if(log.type == 1) "发送:"+log.content else "接收:${log.content}")
    }


    private fun onScannerCap() {
        onScannerCap.setOnTouchListener { v, event ->
            val action = event.action
            when (action) {
                MotionEvent.ACTION_DOWN -> {
                    Blast.getInstance().getScannerLoader().startDecode()
                }
                MotionEvent.ACTION_UP -> {
                    Blast.getInstance().getScannerLoader().stopDecode()
                }
            }
            true
        }

        mainScanResult.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                BlastLog.e(s.toString())
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

        })

    }


    fun onCheckAlongCap(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }
        Blast.getInstance().getAlongCapCheckLoader()
            .start(object : Callback<AlongCapResultEntity>{

                override fun onResult(t: AlongCapResultEntity) {
                    alongCapView.text = "onResult (mVoltage:${t.mVoltage} mElectric:${t.mElectric} uid:${t.uid})"
                }

                override fun onRetryCountChanged(retryCount: Int, action: String) {
                    alongCapView.text = "onRetryCountChanged retryCount:${retryCount}  action:${action}"
                }

                override fun onError(errorCode: ErrorResult) {
                    alongCapView.text = "onError errorCode:${errorCode.errorAction}"
                }

            })

    }

    fun getVersion(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }

        Blast.getInstance().getVersion(object : OnVersionCallback {
            override fun onResult(t: Int) {
                versionView.text = "版本号:${t}"
            }

            override fun onRetryCountChanged(retryCount: Int, action: String) {
                versionView.text = "retryCount:${retryCount}  action:${action}"
            }

            override fun onError(errorCode: ErrorResult) {
                versionView.text = "onError:${errorCode.errorAction}"
            }

        })
    }

    fun upgradeVersion(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }
        CapUtils.readAssetsBinFile(this, Consumer {
            Blast.getInstance().upgrade(35, it, object : OnVersionUpgradeCallback {
                override fun onProgressChanged(progress: Int, total: Int, action: String) {
                    BlastLog.e("progress:${progress}  action:${action}")
                    upgradeVersionView.text = "progress:${progress}  action:${action}"
                }

                override fun onResult(t: Int) {
                    upgradeVersionView.text = "升级操作后的版本号:${t}"
                }

                override fun onRetryCountChanged(retryCount: Int, action: String) {
                    upgradeVersionView.text = "retryCount:${retryCount}  action:${action}"
                }

                override fun onError(errorCode: ErrorResult) {
                    upgradeVersionView.text = "onError:${errorCode.errorAction}"
                }

            })
        })

    }

    fun exitUpgradeMode(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }
        Blast.getInstance().exitUpgradeMode(35, object : Callback<Boolean> {
            override fun onResult(t: Boolean) {
                exitUpgradeProgressView.text = if (t) "退出成功" else "退出失败"
            }

            override fun onRetryCountChanged(retryCount: Int, action: String) {
                exitUpgradeProgressView.text = "retryCount:${retryCount}  action:${action}"
            }

            override fun onError(errorCode: ErrorResult) {
                exitUpgradeProgressView.text = "onError:${errorCode.errorAction}"
            }

        })
    }

    @SuppressLint("SetTextI18n")
    fun onScanCapClick(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }
        caps.clear()
        Blast.getInstance().setMaxSupportCapCount(600)
        Blast.getInstance().scanCap().onScanCap(object : OnScanCapCallback {

            override fun notifyVoltageElectricChanged(mVoltage: Float, mElectric: Float) {

            }

            override fun onProgressChanged(progress: Int, total: Int, action: String) {
                BlastLog.e("progress:${progress}  action:${action}")
                scanCapProgressView.text = "progress:${progress}  action:${action}"
            }

            override fun onResult(t: MutableList<CapEntity>) {
                caps.addAll(t)
                if (t[t.size -1].isScanEnd) {
                    scanCapProgressView.text = "scan caps count ${caps.size}"
                }
            }

            override fun onRetryCountChanged(retryCount: Int, action: String) {
                BlastLog.e("retryCount:${retryCount}  action:${action}")
                scanCapProgressView.text = "retryCount:${retryCount}  action:${action}"
            }

            override fun onError(errorCode: ErrorResult) {
                scanCapProgressView.text = "onError:${errorCode.errorAction}"
            }

        })
    }


    fun onUnderCapClick(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }
        CapUtils.setTestPassword(caps)
        Blast.getInstance().underCap().onUnderCap(caps, object : OnUnderCapCallback {
            override fun notifyVoltageElectricChanged(mVoltage: Float, mElectric: Float) {

            }

            override fun onProgressChanged(progress: Int, total: Int, action: String) {
                BlastLog.e("progress:${progress}  action:${action}")
                underCapProgressView.text = "progress:${progress}  action:${action}"
            }

            override fun onResult(t: ErrorReadCapEntity) {
                if (t.errors.isEmpty()) {
                    underCapProgressView.text = "under caps success"
                } else {
                    underCapProgressView.text =
                        "under caps error count ${t.errors.size}"
                }
            }

            override fun onRetryCountChanged(retryCount: Int, action: String) {
                BlastLog.e("retryCount:${retryCount}  action:${action}")
                underCapProgressView.text = "retryCount:${retryCount}  action:${action}"
            }

            override fun onError(errorCode: ErrorResult) {
                underCapProgressView.text = "onError:${errorCode.errorAction}"
            }

        })
    }


    fun onAuthCapClick(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }
        Blast.getInstance().authCap().onAuthCap(12000,object : OnAuthCallback {

            override fun onAuthErrorResult(result: ErrorReadCapEntity) {
                authCapProgressView.text = "auth error  :${result.errors.size}"
            }

            override fun onProgressChanged(progress: Int, total: Int, action: String) {
                BlastLog.e("progress:${progress}  action:${action}")
                authCapProgressView.text = "progress:${progress}  action:${action}"
            }

            override fun onResult(t: CapProgressEntity) {
                if (t.isEnd) {
                    authCapProgressView.text = "auth caps success"
                }
            }

            override fun onRetryCountChanged(retryCount: Int, action: String) {
                BlastLog.e("retryCount:${retryCount}  action:${action}")
                authCapProgressView.text = "retryCount:${retryCount}  action:${action}"
            }

            override fun onError(errorCode: ErrorResult) {
                authCapProgressView.text = "onError:${errorCode.errorAction}"
            }

        })
    }


    fun onChargeCapClick(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }
        Blast.getInstance().charge().onCharge(object : ProgressCallback<ChargeProgressEntity> {
            override fun onProgressChanged(progress: Int, total: Int, action: String) {
                BlastLog.e("progress:${progress}  action:${action}")
                chargeCapProgressView.text = "progress:${progress}  action:${action}"
            }

            override fun onResult(t: ChargeProgressEntity) {
                if (t.isEnd) {
                    if (t.code == 0) {
                        chargeCapProgressView.text = "charge caps count ${caps.size} success ${t.mElectric} ${t.mVoltage}"
                    } else {
                        chargeCapProgressView.text =
                            "charge caps failed . charge error count ${t.chargeErrCaps.size} , not match count ${t.notMatchCaps.size} , not auth count ${t.notAuthCaps.size}"
                    }
                } else {
                    chargeCapProgressView.text = "charge caps count ${caps.size} ${t.mElectric} ${t.mVoltage}"
                }
            }

            override fun onRetryCountChanged(retryCount: Int, action: String) {
                BlastLog.e("retryCount:${retryCount}  action:${action}")
                chargeCapProgressView.text = "retryCount:${retryCount}  action:${action}"
            }

            override fun onError(errorCode: ErrorResult) {
                chargeCapProgressView.text = "onError:${errorCode.errorAction}"
            }

        }, Consumer<ErrorReadCapEntity> { t ->
            chargeCapProgressView.text = "onError:${t.errors.size}"
        })
    }


    fun onBlastCapClick(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }
        Blast.getInstance().charge().cancel()
        Blast.getInstance().blast().onBlast(12000,object : ProgressCallback<CapProgressEntity> {
            override fun onProgressChanged(progress: Int, total: Int, action: String) {
                BlastLog.e("progress:${progress}  action:${action}")
                blastCapProgressView.text = "progress:${progress}  action:${action}"
            }

            override fun onResult(t: CapProgressEntity) {
                if(t.progress == 100) {
                    blastCapProgressView.text = "blast cap count ${caps.size} success"
                }
            }

            override fun onRetryCountChanged(retryCount: Int, action: String) {
                BlastLog.e("retryCount:${retryCount}  action:${action}")
                blastCapProgressView.text = "retryCount:${retryCount}  action:${action}"
            }

            override fun onError(errorCode: ErrorResult) {
                blastCapProgressView.text = "onError:${errorCode.errorAction}"
            }

        },object : Consumer<ErrorReadCapEntity>{
            override fun accept(t: ErrorReadCapEntity) {
                blastCapProgressView.text = "onError:${t.errors[0].error}"
            }
        })

    }


    fun onQuickUnderAuthClick(view: View) {
        if (!hasPermission()) {
            reqPermissions()
            return
        }
        CapUtils.setTestPassword(caps)
        Blast.getInstance().setMaxSupportCapCount(caps.size)
        Blast.getInstance().quickRegister()
            .onQuickRegister(caps, 12000,object : ProgressCallback<CapProgressEntity> {
                override fun onProgressChanged(progress: Int, total: Int, action: String) {
                    BlastLog.e("progress:${progress}  action:${action}")
                    quickCapProgressView.text = "progress:${progress}  action:${action}"
                }

                override fun onResult(t: CapProgressEntity) {
                    if (t.isEnd) {
                        quickCapProgressView.text = "under auth caps success"
                    }
                }

                override fun onRetryCountChanged(retryCount: Int, action: String) {
                    quickCapProgressView.text = "retryCount:${retryCount}  action:${action}"
                }

                override fun onError(errorCode: ErrorResult) {
                    quickCapProgressView.text = "onError:${errorCode.errorAction}"
                }
            }, Consumer {
                quickCapProgressView.text = "onError:${it.errors.size}"
            })

    }


    /**
     * 检查固件
     */
    fun onCheckFirmware(view: View) {
        CapUtils.readAssetsBinFile(this, Consumer {
            Blast.getInstance().getCheckFirmwareLoader()
                .onCheckFirmware(35,it,object : OnCheckFirmwareStateCallback{

                    override fun onCheckFirmwareUpgradeState(isOk: Boolean) {
                        checkFirmwareView.text = if(isOk) "固件正常" else "固件异常"
                    }

                    override fun onProgressChanged(progress: Int, total: Int, action: String) {
                        checkFirmwareView.text = "progress:${progress}  action:${action}"
                    }

                })
        })
    }


    fun onParamClick(view: View) {
        val param = Param()
        param.signalVoltage = 85
        param.chargeVoltage = 200
        param.queryVoltage = 120
        param.chargeAdc = 18
        param.blastAdc = 12
        param.blastCutPower = 0
        param.groupChargeCount = 10
        param.groupChargeSleepTime = 30
        param.reserve1 =50
        param.reserve2 =0
        param.reserve3 =0
        param.reserve4 =0

        Blast.getInstance().getParamLoader()
            .update(param,
                Consumer<Boolean> {
                    if(it) {
                        Blast.getInstance().getParamLoader().read(
                            Consumer<Param> {
                                BlastLog.e(it.toString())
                            })
                    } else {
                        BlastLog.e("修改失败")
                    }
                })
    }


    override fun onDestroy() {
        super.onDestroy()
        Blast.getInstance().getScannerLoader().closeScanner(this)
        EventBus.getDefault().unregister(this)
        unregisterReceiver(mScreenStatusReceiver);
    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {

    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {

    }


    private var mScreenStatusReceiver: ScreenStatusReceiver? = null


    class ScreenStatusReceiver : BroadcastReceiver() {
        private val SCREEN_ON = "android.intent.action.SCREEN_ON"
        private val SCREEN_OFF = "android.intent.action.SCREEN_OFF"
        override fun onReceive( context:Context,  intent:Intent) {
            if (SCREEN_ON.equals(intent.getAction())) {
//                Blast.getInstance().enableOtg(true)
            }
            else if (SCREEN_OFF.equals(intent.getAction())) {
//                Blast.getInstance().enableOtg(false)
            }
        }}


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onPioUnEnableEvent(e: PioUnEnableEvent){
        BlastLog.e("PioUnEnableEvent")
    }
}
